package com.generate;

import java.awt.BorderLayout;
import java.awt.Button;
import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.Label;
import java.awt.Panel;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;
import javax.swing.event.PopupMenuEvent;
import javax.swing.event.PopupMenuListener;

import com.generate.control.JComboboxReplace;
import com.generate.control.JPanelVariant;
import com.generate.control.JTextFieldReplace;
import com.generate.library.GenerateUtils;
import com.generate.library.ImpexUtils;

public class MainForm {
	private static final String FOLDER_IMPEX_LOAD = "FOLDER_IMPEX_LOAD";
	private Properties properties = new Properties();

	private JFrame frame;
	private JPanelVariant panelVariant_1;
	private JPanelVariant panelVariant_2;
	private JPanelVariant panelVariant_3;
	private JPanelVariant panelVariant_4;
	private JPanelVariant panelVariant_5;
	private JPanelVariant panelVariant_6;
	private JPanelVariant panelVariant_7;
	private JPanelVariant panelVariant_8;
	private JPanel panel_1;
	private JPanel panel_2;
	private JPanel panel_3;
	private JPanel panel_4;
	private JPanel panel_5;
	private JPanel panel_6;
	private JPanel panel_7;
	private JPanel panel_8;
	private JPanel panel;
	private JTextField tfLoadImpex;
	private final JFileChooser fileChooserLoadImpex = new JFileChooser();
	private JTabbedPane tabbedPane;
	private JComboboxReplace cbbVariantNumber;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainForm window = new MainForm();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	private void onVariantNumberChange() {
		Long numberVariant = Long.parseLong(cbbVariantNumber.getSelectedItem()
				.toString());
		tabbedPane.remove(panel_1);
		tabbedPane.remove(panel_2);
		tabbedPane.remove(panel_3);
		tabbedPane.remove(panel_4);
		tabbedPane.remove(panel_5);
		tabbedPane.remove(panel_6);
		tabbedPane.remove(panel_7);
		tabbedPane.remove(panel_8);

		if (numberVariant >= 1) {
			tabbedPane.add(panel_1, "Var 1");
		}
		if (numberVariant >= 2) {
			tabbedPane.add(panel_2, "Var 2");
		}
		if (numberVariant >= 3) {
			tabbedPane.add(panel_3, "Var 3");
		}
		if (numberVariant >= 4) {
			tabbedPane.add(panel_4, "Var 4");
		}
		if (numberVariant >= 5) {
			tabbedPane.add(panel_5, "Var 5");
		}
		if (numberVariant >= 6) {
			tabbedPane.add(panel_6, "Var 6");
		}
		if (numberVariant >= 7) {
			tabbedPane.add(panel_7, "Var 7");
		}
		if (numberVariant >= 8) {
			tabbedPane.add(panel_8, "Var 8");
		}
	}

	/**
	 * Create the application.
	 */
	public MainForm() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.addWindowListener(new WindowAdapter() {
			@Override
			public void windowActivated(WindowEvent arg0) {
				addListenner();
			}
		});
		frame.setBounds(100, 100, 675, 513);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		frame.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		
				final Panel panelConfig = new Panel();
				tabbedPane.addTab("Config", null, panelConfig, null);
				GridBagLayout gbl_panel_11 = new GridBagLayout();
				gbl_panel_11.columnWidths = new int[] { 0, 0, 0, 0 };
				gbl_panel_11.rowHeights = new int[] { 0, 0, 0 };
				gbl_panel_11.columnWeights = new double[] { 0.0, 1.0, 0.0,
						Double.MIN_VALUE };
				gbl_panel_11.rowWeights = new double[] { 0.0, 0.0, Double.MIN_VALUE };
				panelConfig.setLayout(gbl_panel_11);
				
						JLabel lblLoadFromFile = new JLabel("Load impex");
						GridBagConstraints gbc_lblLoadFromFile = new GridBagConstraints();
						gbc_lblLoadFromFile.insets = new Insets(0, 0, 5, 5);
						gbc_lblLoadFromFile.anchor = GridBagConstraints.EAST;
						gbc_lblLoadFromFile.gridx = 0;
						gbc_lblLoadFromFile.gridy = 0;
						panelConfig.add(lblLoadFromFile, gbc_lblLoadFromFile);
						
								tfLoadImpex = new JTextField();
								tfLoadImpex.setEditable(false);
								GridBagConstraints gbc_tfLoadImpex = new GridBagConstraints();
								gbc_tfLoadImpex.insets = new Insets(0, 0, 5, 5);
								gbc_tfLoadImpex.fill = GridBagConstraints.HORIZONTAL;
								gbc_tfLoadImpex.gridx = 1;
								gbc_tfLoadImpex.gridy = 0;
								panelConfig.add(tfLoadImpex, gbc_tfLoadImpex);
								tfLoadImpex.setColumns(10);
								
										JButton btnLoadFile = new JButton("Load file");
										btnLoadFile.addActionListener(new ActionListener() {
											public void actionPerformed(ActionEvent e) {
												if (fileChooserLoadImpex.showOpenDialog(panelConfig) == JFileChooser.APPROVE_OPTION) {
													File fileImpexLoaded = fileChooserLoadImpex
															.getSelectedFile();
													tfLoadImpex.setText(fileImpexLoaded.getPath());
													ImpexUtils.loadFolder(fileImpexLoaded);
													onVariantNumberChange();
													try {
														setProperty(FOLDER_IMPEX_LOAD,
																fileImpexLoaded.getParent());
													} catch (FileNotFoundException e1) {
														e1.printStackTrace();
													} catch (IOException e1) {
														e1.printStackTrace();
													}
												}
											}
										});
										GridBagConstraints gbc_btnLoadFile = new GridBagConstraints();
										gbc_btnLoadFile.insets = new Insets(0, 0, 5, 0);
										gbc_btnLoadFile.gridx = 2;
										gbc_btnLoadFile.gridy = 0;
										panelConfig.add(btnLoadFile, gbc_btnLoadFile);
										
										JLabel lblMssext = new JLabel(" Msscn Folder");
										GridBagConstraints gbc_lblMssext = new GridBagConstraints();
										gbc_lblMssext.insets = new Insets(0, 0, 0, 5);
										gbc_lblMssext.anchor = GridBagConstraints.EAST;
										gbc_lblMssext.gridx = 0;
										gbc_lblMssext.gridy = 1;
										panelConfig.add(lblMssext, gbc_lblMssext);
										
										JTextFieldReplace tfWorkspaceFolder = new JTextFieldReplace();
										GridBagConstraints gbc_tfWorkspaceFolder = new GridBagConstraints();
										gbc_tfWorkspaceFolder.insets = new Insets(0, 0, 0, 5);
										gbc_tfWorkspaceFolder.fill = GridBagConstraints.HORIZONTAL;
										gbc_tfWorkspaceFolder.gridx = 1;
										gbc_tfWorkspaceFolder.gridy = 1;
										panelConfig.add(tfWorkspaceFolder, gbc_tfWorkspaceFolder);

		panel = new JPanel();
		tabbedPane.addTab("Base product", null, panel, null);
		GridBagLayout gbl_panel = new GridBagLayout();
		gbl_panel.columnWidths = new int[] { 0, 0, 0 };
		gbl_panel.rowHeights = new int[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
		gbl_panel.columnWeights = new double[] { 0.0, 1.0, Double.MIN_VALUE };
		gbl_panel.rowWeights = new double[] { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
				0.0, 0.0, 0.0, Double.MIN_VALUE };
		panel.setLayout(gbl_panel);

		Label label = new Label("Electronics Color Variant Product Type");
		GridBagConstraints gbc_label = new GridBagConstraints();
		gbc_label.anchor = GridBagConstraints.EAST;
		gbc_label.insets = new Insets(0, 0, 5, 5);
		gbc_label.gridx = 0;
		gbc_label.gridy = 0;
		panel.add(label, gbc_label);

		JComboboxReplace comboboxReplace_1 = new JComboboxReplace();
		comboboxReplace_1
				.setFindText("@ELECTRONICS_COLOR_VARIANT_PRODUCT_TYPE@");
		comboboxReplace_1.setModel(new DefaultComboBoxModel<String>(
				new String[] { "ACCESSORY", "OFFICE" }));
		GridBagConstraints gbc_comboboxReplace_1 = new GridBagConstraints();
		gbc_comboboxReplace_1.insets = new Insets(0, 0, 5, 0);
		gbc_comboboxReplace_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboboxReplace_1.gridx = 1;
		gbc_comboboxReplace_1.gridy = 0;
		panel.add(comboboxReplace_1, gbc_comboboxReplace_1);

		Label label_1 = new Label("super_categories");
		GridBagConstraints gbc_label_1 = new GridBagConstraints();
		gbc_label_1.anchor = GridBagConstraints.EAST;
		gbc_label_1.insets = new Insets(0, 0, 5, 5);
		gbc_label_1.gridx = 0;
		gbc_label_1.gridy = 1;
		panel.add(label_1, gbc_label_1);

		JComboboxReplace comboboxReplace = new JComboboxReplace();
		comboboxReplace.setFindText("@SUPER_CATEGORIES@");
		comboboxReplace.setModel(new DefaultComboBoxModel<String>(new String[] {
				"surface_accessories", "office" }));
		GridBagConstraints gbc_comboboxReplace = new GridBagConstraints();
		gbc_comboboxReplace.insets = new Insets(0, 0, 5, 0);
		gbc_comboboxReplace.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboboxReplace.gridx = 1;
		gbc_comboboxReplace.gridy = 1;
		panel.add(comboboxReplace, gbc_comboboxReplace);

		Label label_2 = new Label("Sale status");
		GridBagConstraints gbc_label_2 = new GridBagConstraints();
		gbc_label_2.anchor = GridBagConstraints.EAST;
		gbc_label_2.insets = new Insets(0, 0, 5, 5);
		gbc_label_2.gridx = 0;
		gbc_label_2.gridy = 2;
		panel.add(label_2, gbc_label_2);

		JComboboxReplace comboboxReplace_2 = new JComboboxReplace();
		comboboxReplace_2.setFindText("@SALE_STATUS@");
		comboboxReplace_2.setModel(new DefaultComboBoxModel<String>(
				new String[] { "FOR_SALE", "MAKETING" }));
		GridBagConstraints gbc_comboboxReplace_2 = new GridBagConstraints();
		gbc_comboboxReplace_2.insets = new Insets(0, 0, 5, 0);
		gbc_comboboxReplace_2.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboboxReplace_2.gridx = 1;
		gbc_comboboxReplace_2.gridy = 2;
		panel.add(comboboxReplace_2, gbc_comboboxReplace_2);

		Label label_6 = new Label("Variant number");
		GridBagConstraints gbc_label_6 = new GridBagConstraints();
		gbc_label_6.anchor = GridBagConstraints.EAST;
		gbc_label_6.insets = new Insets(0, 0, 5, 5);
		gbc_label_6.gridx = 0;
		gbc_label_6.gridy = 3;
		panel.add(label_6, gbc_label_6);

		cbbVariantNumber = new JComboboxReplace();
		cbbVariantNumber.addPopupMenuListener(new PopupMenuListener() {
			public void popupMenuCanceled(PopupMenuEvent arg0) {
			}

			public void popupMenuWillBecomeInvisible(PopupMenuEvent arg0) {
				onVariantNumberChange();
			}

			public void popupMenuWillBecomeVisible(PopupMenuEvent arg0) {
			}
		});
		cbbVariantNumber.setFindText("@VARIANT_NUMBER@");
		cbbVariantNumber.setModel(new DefaultComboBoxModel<String>(
				new String[] { "1", "2", "3", "4", "5", "6", "7", "8" }));
		GridBagConstraints gbc_comboboxReplace_3 = new GridBagConstraints();
		gbc_comboboxReplace_3.insets = new Insets(0, 0, 5, 0);
		gbc_comboboxReplace_3.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboboxReplace_3.gridx = 1;
		gbc_comboboxReplace_3.gridy = 3;
		panel.add(cbbVariantNumber, gbc_comboboxReplace_3);

		Label label_7 = new Label("Image detail number");
		GridBagConstraints gbc_label_7 = new GridBagConstraints();
		gbc_label_7.anchor = GridBagConstraints.EAST;
		gbc_label_7.insets = new Insets(0, 0, 5, 5);
		gbc_label_7.gridx = 0;
		gbc_label_7.gridy = 4;
		panel.add(label_7, gbc_label_7);

		JComboboxReplace comboboxReplace_4 = new JComboboxReplace();
		comboboxReplace_4.setModel(new DefaultComboBoxModel<String>(
				new String[] { "1", "2", "3" }));
		comboboxReplace_4.setFindText("@IMAGE_DETAIL_NUMBER@");
		GridBagConstraints gbc_comboboxReplace_4 = new GridBagConstraints();
		gbc_comboboxReplace_4.insets = new Insets(0, 0, 5, 0);
		gbc_comboboxReplace_4.fill = GridBagConstraints.HORIZONTAL;
		gbc_comboboxReplace_4.gridx = 1;
		gbc_comboboxReplace_4.gridy = 4;
		panel.add(comboboxReplace_4, gbc_comboboxReplace_4);

		Panel panel_9 = new Panel();
		GridBagConstraints gbc_panel_9 = new GridBagConstraints();
		gbc_panel_9.insets = new Insets(0, 0, 5, 0);
		gbc_panel_9.gridwidth = 2;
		gbc_panel_9.gridx = 0;
		gbc_panel_9.gridy = 5;
		panel.add(panel_9, gbc_panel_9);
		GridBagLayout gbl_panel_9 = new GridBagLayout();
		gbl_panel_9.columnWidths = new int[] { 0 };
		gbl_panel_9.rowHeights = new int[] { 0 };
		gbl_panel_9.columnWeights = new double[] { Double.MIN_VALUE };
		gbl_panel_9.rowWeights = new double[] { Double.MIN_VALUE };
		panel_9.setLayout(gbl_panel_9);

		Label label_3 = new Label("baseProductCode");
		GridBagConstraints gbc_label_3 = new GridBagConstraints();
		gbc_label_3.anchor = GridBagConstraints.EAST;
		gbc_label_3.insets = new Insets(0, 0, 5, 5);
		gbc_label_3.gridx = 0;
		gbc_label_3.gridy = 6;
		panel.add(label_3, gbc_label_3);

		JTextFieldReplace txtfldrplcbaseproductcode = new JTextFieldReplace();
		txtfldrplcbaseproductcode.setFindText("@BASE_PRODUCT_CODE@");
		GridBagConstraints gbc_txtfldrplcbaseproductcode = new GridBagConstraints();
		gbc_txtfldrplcbaseproductcode.insets = new Insets(0, 0, 5, 0);
		gbc_txtfldrplcbaseproductcode.fill = GridBagConstraints.HORIZONTAL;
		gbc_txtfldrplcbaseproductcode.gridx = 1;
		gbc_txtfldrplcbaseproductcode.gridy = 6;
		panel.add(txtfldrplcbaseproductcode, gbc_txtfldrplcbaseproductcode);

		Label label_4 = new Label("baseProductName");
		GridBagConstraints gbc_label_4 = new GridBagConstraints();
		gbc_label_4.anchor = GridBagConstraints.EAST;
		gbc_label_4.insets = new Insets(0, 0, 5, 5);
		gbc_label_4.gridx = 0;
		gbc_label_4.gridy = 7;
		panel.add(label_4, gbc_label_4);

		JTextFieldReplace textFieldReplace = new JTextFieldReplace();
		textFieldReplace.setFindText("@BASE_PRODUCT_NAME@");
		GridBagConstraints gbc_textFieldReplace = new GridBagConstraints();
		gbc_textFieldReplace.insets = new Insets(0, 0, 5, 0);
		gbc_textFieldReplace.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldReplace.gridx = 1;
		gbc_textFieldReplace.gridy = 7;
		panel.add(textFieldReplace, gbc_textFieldReplace);

		Label label_5 = new Label("baseProductPrices");
		GridBagConstraints gbc_label_5 = new GridBagConstraints();
		gbc_label_5.anchor = GridBagConstraints.EAST;
		gbc_label_5.insets = new Insets(0, 0, 0, 5);
		gbc_label_5.gridx = 0;
		gbc_label_5.gridy = 8;
		panel.add(label_5, gbc_label_5);

		JTextFieldReplace textFieldReplace_1 = new JTextFieldReplace();
		textFieldReplace_1.setFindText("@BASE_PRODUCT_PRICES@");
		GridBagConstraints gbc_textFieldReplace_1 = new GridBagConstraints();
		gbc_textFieldReplace_1.fill = GridBagConstraints.HORIZONTAL;
		gbc_textFieldReplace_1.gridx = 1;
		gbc_textFieldReplace_1.gridy = 8;
		panel.add(textFieldReplace_1, gbc_textFieldReplace_1);

		fileChooserLoadImpex.setDialogTitle("Load impex");
		GridBagConstraints gbc_fileChooser = new GridBagConstraints();
		gbc_fileChooser.insets = new Insets(0, 0, 0, 5);
		gbc_fileChooser.gridx = 1;
		gbc_fileChooser.gridy = 1;

		panel_1 = new JPanel();
		tabbedPane.addTab("Var 1", null, panel_1, null);
		panel_1.setLayout(new BorderLayout(0, 0));

		panelVariant_1 = new JPanelVariant(1);
		panel_1.add(panelVariant_1);

		panel_2 = new JPanel();
		tabbedPane.addTab("Var 2", null, panel_2, null);
		panel_2.setLayout(new BorderLayout(0, 0));

		panelVariant_2 = new JPanelVariant(2);
		panel_2.add(panelVariant_2, BorderLayout.CENTER);

		panel_3 = new JPanel();
		tabbedPane.addTab("Var 3", null, panel_3, null);
		panel_3.setLayout(new BorderLayout(0, 0));

		panelVariant_3 = new JPanelVariant(3);
		panel_3.add(panelVariant_3);

		panel_4 = new JPanel();
		tabbedPane.addTab("Var 4", null, panel_4, null);
		panel_4.setLayout(new BorderLayout(0, 0));

		panelVariant_4 = new JPanelVariant(4);
		panel_4.add(panelVariant_4, BorderLayout.CENTER);

		panel_5 = new JPanel();
		tabbedPane.addTab("Var 5", null, panel_5, null);
		panel_5.setLayout(new BorderLayout(0, 0));

		panelVariant_5 = new JPanelVariant(5);
		panel_5.add(panelVariant_5, BorderLayout.CENTER);

		panel_6 = new JPanel();
		tabbedPane.addTab("Var 6", null, panel_6, null);
		panel_6.setLayout(new BorderLayout(0, 0));

		panelVariant_6 = new JPanelVariant(6);
		panel_6.add(panelVariant_6, BorderLayout.CENTER);

		panel_7 = new JPanel();
		tabbedPane.addTab("Var 7", null, panel_7, null);
		panel_7.setLayout(new BorderLayout(0, 0));

		panelVariant_7 = new JPanelVariant(7);
		panel_7.add(panelVariant_7, BorderLayout.CENTER);

		panel_8 = new JPanel();
		tabbedPane.addTab("Var 8", null, panel_8, null);
		panel_8.setLayout(new BorderLayout(0, 0));

		panelVariant_8 = new JPanelVariant(8);
		panel_8.add(panelVariant_8, BorderLayout.CENTER);

		tabbedPane.remove(panel_8);
		tabbedPane.remove(panel_7);
		tabbedPane.remove(panel_6);
		tabbedPane.remove(panel_5);
		tabbedPane.remove(panel_4);
		tabbedPane.remove(panel_3);
		tabbedPane.remove(panel_2);

		Panel panel_10 = new Panel();
		frame.getContentPane().add(panel_10, BorderLayout.SOUTH);

		Button button = new Button("Export");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				try {
					if (fileChooserLoadImpex.showSaveDialog(panelConfig) == JFileChooser.APPROVE_OPTION) {
						File fileImpexLoaded = fileChooserLoadImpex
								.getSelectedFile();
						try {
							GenerateUtils.generate(fileImpexLoaded);
						} catch (Exception iox) {
							iox.printStackTrace();
						}
						try {
							setProperty(FOLDER_IMPEX_LOAD,
									fileImpexLoaded.getParent());
						} catch (FileNotFoundException e1) {
							e1.printStackTrace();
						} catch (IOException e1) {
							e1.printStackTrace();
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		panel_10.add(button);
	}

	private void setProperty(String key, String value)
			throws FileNotFoundException, IOException {
		properties.setProperty(key, value);
		properties.store(new FileOutputStream("config.properties"), null);
	}

	private void addListenner() {
		try {
			properties.load(new FileInputStream("config.properties"));
		} catch (Exception e) {
			try {
				properties.store(new FileOutputStream("config.properties"),
						null);
			} catch (Exception e1) {
				e1.printStackTrace();
			}
		}
		fileChooserLoadImpex.setCurrentDirectory(new File(properties
				.getProperty(FOLDER_IMPEX_LOAD)));
		fileChooserLoadImpex
				.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		fileChooserLoadImpex.setAcceptAllFileFilterUsed(false);
	}

}
